﻿Imports DTO
Imports System.Windows.Forms

Public Class ChiTietHDDAO
    Public Sub Them(ByVal cthdDTO As ChiTietHDDTO)
        Try
            Dim db As New QuanLyNhaSachDataContext
            Dim cthd As New ChiTietHD
            cthd.MaCTHD = cthdDTO.MaCTHD
            cthd.MaHD = cthdDTO.MaHD
            cthd.TenSach = cthdDTO.TenSach
            cthd.MaTL = cthdDTO.MaTL
            cthd.SoLuong = cthdDTO.SoLuong
            cthd.DonGiaBan = cthdDTO.DonGiaBan
            cthd.ThanhTien = cthdDTO.ThanhTien

            db.ChiTietHDs.InsertOnSubmit(cthd)
            db.SubmitChanges()
            cthdDTO.MaCTHD = cthd.MaCTHD
        Catch ex As Exception
            'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Function TimKiem(ByVal cthdDTO As ChiTietHDDTO) As IQueryable(Of ChiTietHD)
        Dim db As New QuanLyNhaSachDataContext
        Dim cthds = (From ct In db.ChiTietHDs _
                    Where ct.MaHD = cthdDTO.MaHD And ct.TenSach = cthdDTO.TenSach _
                    And ct.MaTL = cthdDTO.MaTL And ct.TenTG = cthdDTO.TenTG _
                    Select ct)
        Try
            cthdDTO.MaCTHD = cthds.Single.MaCTHD
        Catch ex As Exception
        End Try

        Return cthds
    End Function

    Public Sub CapNhat(ByVal cthdDTO As ChiTietHDDTO)
        Dim db As New QuanLyNhaSachDataContext
        Dim cthd = db.ChiTietHDs.Single(Function(ct) ct.MaCTHD = cthdDTO.MaCTHD)

        cthd.TenSach = cthdDTO.TenSach
        cthd.MaTL = cthdDTO.MaTL
        cthd.SoLuong = cthdDTO.SoLuong
        cthd.DonGiaBan = cthdDTO.DonGiaBan

        db.SubmitChanges()
    End Sub
End Class
